#!/bin/bash

# -------------------------------------------------------------------------------------
# Docker 操作脚本函数定义文件
# -------------------------------------------------------------------------------------

# -------------------------------------------------------------------------------------
# 读取 Env 配置
# -------------------------------------------------------------------------------------
function loadEnv()
{
  param=$1
  default_value=$2
  value=$(sed -E '/^#.*|^ *$/d' '.env' | awk -F "${param}.*=" "/${param}.*=/{print \$2}" | sed -e 's/^[ \t]*//g' -e 's/[ \t]*$//g' -e 's/^[\r]*//g' -e 's/[\r]*$//g')

  if [ -n "$value" ]
  then
    echo $value
  else
    echo $default_value
  fi
}

# -------------------------------------------------------------------------------------
# 构建镜像
# -------------------------------------------------------------------------------------
function builds()
{
     # 构建镜像
    docker build -t website:$DOCKER_IMAGE_NAME \
    --build-arg PHP_VERSION=$DOCKER_PHP_VERSION \
    "$DOCKER_WORK_PATH/.phpdocker/"
}

# -------------------------------------------------------------------------------------
# 运行容器
# -------------------------------------------------------------------------------------
function runing()
{
    # 运行镜像
    docker run -d --name $DOCKER_NAME -p "${LOCAL_PORT}:80" \
    --mount source=$DOCKER_WORK_PATH/,target=$DOCKER_WORKSPACE,type=bind,consistency=cached \
    --mount source=$DOCKER_WORK_PATH/.phpdocker/nginx.conf,target=/etc/nginx/nginx.conf,type=bind,consistency=cached \
    --network=database_app website:$DOCKER_IMAGE_NAME
}

# -------------------------------------------------------------------------------------
# 启动 Nginx
# -------------------------------------------------------------------------------------
function RunNginx()
{
    # 启动 Nginx
    docker exec -it -u root $DOCKER_NAME /bin/sh -c nginx
}

# -------------------------------------------------------------------------------------
# 启动容器
# -------------------------------------------------------------------------------------
function start()
{
    image_name=$(docker image ls | grep $DOCKER_IMAGE_NAME | awk '{print $1}')
    if [ -z "$image_name" ]; then
        # 构建镜像
        builds
        runing
    else
        is_start=$(docker container ps | grep $DOCKER_NAME | awk '{print $1}')
        if [ -z "$is_start" ]; then
            is_start=$(docker container ps -a | grep $DOCKER_NAME | awk '{print $1}')
            if [ -z "$is_start" ]; then
                # 运行镜像
                runing
            else
                docker container start $DOCKER_NAME
                echo 'start...'
            fi
        else
            docker container restart $DOCKER_NAME
            echo 'restart...'
        fi
    fi
}

# -------------------------------------------------------------------------------------
# 停止容器
# -------------------------------------------------------------------------------------
function stop()
{
    is_start=$(docker container ps | grep $DOCKER_NAME | awk '{print $1}')
    if [ -z "$is_start" ]; then
        echo "$DOCKER_NAME is not runing"
    else
        docker container stop $DOCKER_NAME
        echo 'stop...'
    fi
}

# -------------------------------------------------------------------------------------
# 删除容器
# -------------------------------------------------------------------------------------
function remove()
{
    is_start=$(docker container ps | grep $DOCKER_NAME | awk '{print $1}')
    if [ ! -z "$is_start" ]; then
        docker container stop $DOCKER_NAME
        docker container rm $DOCKER_NAME
    else
        d_container=$(docker container ps -a | grep $DOCKER_NAME | awk '{print $1}')
        if [ ! -z "$d_container" ]; then
            docker container rm $DOCKER_NAME
        fi
    fi

    image_name=$(docker image ls | grep $DOCKER_IMAGE_NAME | awk '{print $1}')
    if [ ! -z "$image_name" ]; then
        docker image rm website:$DOCKER_IMAGE_NAME
    fi
}

# -------------------------------------------------------------------------------------
# 进入容器
# -------------------------------------------------------------------------------------
function exec()
{
    is_start=$(docker container ps | grep $DOCKER_NAME | awk '{print $1}')
    if [ -z "$is_start" ]; then
        echo "${DOCKER_NAME} is not runing"
    else
        docker exec -it -u root -e PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -w $DOCKER_WORKSPACE $DOCKER_NAME sh
    fi
}

# -------------------------------------------------------------------------------------
# 导出容器
# -------------------------------------------------------------------------------------
function export()
{
    image=$(docker container ps -a | grep $DOCKER_IMAGE_NAME | awk '{print $1}')
    if [ ! -z "$image" ]; then
      docker export $image > "${DOCKER_IMAGE_NAME}.tar"
      echo "export ${DOCKER_IMAGE_NAME} image success"
    else
      echo "${DOCKER_IMAGE_NAME} image not find"
    fi
}

# -------------------------------------------------------------------------------------
# 导入容器
# -------------------------------------------------------------------------------------
function import()
{
    image=$(docker image ls | grep $DOCKER_IMAGE_NAME | awk '{print $1}')
    if [ -z "$image" ]; then
      cat "${DOCKER_IMAGE_NAME}.tar" | docker import - website:$DOCKER_IMAGE_NAME
      echo "import ${DOCKER_IMAGE_NAME}.tar image success"
    else
      echo "${DOCKER_IMAGE_NAME} image already exists"
    fi
}

# -------------------------------------------------------------------------------------
# 帮助函数
# -------------------------------------------------------------------------------------
help() {
    echo "docker-run [option]"
    echo "  start       构建并运行容器"
    echo "  restart     重启容器"
    echo "  stop        停止容器"
    echo "  remove      删除容器和镜像"
    echo "  exec        进入容器"
    echo "  export      导出镜像"
    echo "  import      导入镜像"
    echo "  help        帮助信息"
}

# -------------------------------------------------------------------------------------
# 检查是否添加Docker网络
# -------------------------------------------------------------------------------------
checkNetwork() {
  is_add=$(docker network ls | grep 'database_app' | awk '{print $1}')
  if [ -z "$is_add" ]; then
    docker network create database_app
  fi
}

# -------------------------------------------------------------------------------------
# 检查绑定到主机的端口是否被其他容器占用
# 如果被占用则停止占用的容器
# -------------------------------------------------------------------------------------
function checkPort()
{
    container_id=$(docker container ps | grep ":${LOCAL_PORT}->" | awk '{print $1}')
    container_name=$(docker container ps | grep ":${LOCAL_PORT}->" | awk '{print $NF}')

    if [[ "$container_name" != "$DOCKER_NAME" && -n "$container_id" ]]; then
        docker container stop $container_id
        echo 'stop...'
    fi
}